<template>
  <!-- 学校校区管理 -->
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        class="formBox"
        label-width="100px"
      >
        <el-form-item label="商户名称" prop="merchantName">
          <el-input placeholder="请输入商户名称" v-model="queryParams.merchantName" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 按钮 -->
    <el-card class="box-card" style="margin-top: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="buttonMethod(1)"
            v-hasPermi="['systemSet:merchantInfo:add']"
            >新增
          </el-button>
        </el-col>

        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <!-- 列表 -->
    <el-card class="table-card" style="margin-top: 10px">
      <el-table :data="dataList" stripe border>
        <el-table-column
          label="商户名称"
          align="center"
          prop="merchantName"
          width="200"
        />
        <el-table-column label="支付套餐" align="center" prop="payWayName" />
        <el-table-column label="开户行" align="center" prop="bankName" />
        <el-table-column label="开户账号" align="center" prop="bankAccount" />
        <el-table-column label="乐易呗商编" align="center" prop="merchantId" />
        <el-table-column label="乐易呗通道商编" align="center" prop="merchantCode" />
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '1'">启用</el-tag>
            <el-tag v-if="scope.row.status == '2'" type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(1, scope.row)"
              v-hasPermi="['systemSet:merchantInfo:edit']"
              >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="buttonMethod(5, scope.row)"
              v-hasPermi="['systemSet:merchantInfo:status']"
            >
              <span v-if="scope.row.status == 1">停用</span>
              <span v-if="scope.row.status == 2">启用</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加、编辑 -->
    <el-dialog :title="title" :visible.sync="status" width="30%">
      <el-form label-width="160px" size="small" class="formBox">
        <el-row class="mb_10">
          <el-col :span="20">
            <el-form-item label="商户名称：" class="is-required">
              <el-input
                placeholder="请输入商户名称"
                v-model="queryForm.merchantName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="开户行：" class="is-required">
              <el-input placeholder="请输入开户行" v-model="queryForm.bankName" />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="账户号：" class="is-required">
              <el-input placeholder="请输入账户号" v-model="queryForm.bankAccount" />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="乐易呗商编：" class="is-required">
              <el-input placeholder="请输入乐易呗商编" v-model="queryForm.merchantId" />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="乐易呗通道商编：" class="is-required">
              <el-input
                placeholder="请输入乐易呗通道商编"
                v-model="queryForm.merchantCode"
              />
            </el-form-item>
          </el-col>

          <el-col :span="20">
            <el-form-item label="学校支付套餐名称：" class="is-required">
              <el-select
                v-model="queryForm.payWayId"
                placeholder="请选择学校支付套餐名称"
               
                class="width_100"
              >
                <el-option
                  v-for="(item, index) in schoolPayWayListData"
                  :key="index"
                  :label="item.schoolPayWay"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="状态：" class="is-required">
              <el-radio-group v-model="queryForm.status">
                <el-radio :label="1">正常</el-radio>
                <el-radio :label="2">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="buttonMethod(2)">取 消</el-button>
        <el-button type="primary" @click="buttonMethod(3)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  merMerchant,
  schoolPayWayList,
  merMerchantPost,
  merMerchantPut,
} from "@/api/systemSet";
export default {
  name: "",
  data() {
    return {
      status: false,
      title: "",
      dataList: [],
      queryParams: {
        merchantName: null,
        pageNum: 1,
        pageSize: 10,
      },
      queryForm: {},
      total: 1,
      showSearch: true,
      schoolPayWayListData: [],
    };
  },
  created() {
    this.getList();
    this.schoolPayWayListFun();
  },
  methods: {
    // 获取列表数据
    getList() {
      var data = this.queryParams;
      merMerchant(data).then((res) => {
        if (res.code == 200) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    /**
     * 获取学校支付方式列表
     */
    schoolPayWayListFun() {
      schoolPayWayList().then((res) => {
        if (res.code == 200) {
          this.schoolPayWayListData = res.data;
        }
      });
    },
    /**
     * 按钮点击事件处理方法
     */
    buttonMethod(options, value) {
      //options 等于 1
      if (options == 1) {
        if (value) {
          this.queryForm = { ...value };
          this.title = "修改";
        } else {
          this.queryForm = {};
          this.title = "添加";
        }
        this.status = true;
      }
      //options 等于 2 关闭弹窗
      else if (options == 2) {
        this.queryForm = {};
        this.title = "";
        this.status = false;
      }
      //options 等于 3 关闭弹窗
      else if (options == 3) {
        var data = this.queryForm;
        if (!data.merchantName) {
          return this.$message.error("请输入商户名称");
        }
        if (!data.payWayId) {
          return this.$message.error("请选择支付套餐名称");
        }
        if (!data.bankName) {
          return this.$message.error("请输入开户行");
        }
        if (!data.bankAccount) {
          return this.$message.error("请输入银行卡号");
        }
        if (!data.merchantId) {
          return this.$message.error("请输入乐易呗商编");
        }
        if (!data.merchantCode) {
          return this.$message.error("请输入乐易呗通道商编");
        }
        if (!data.status) {
          return this.$message.error("请选择状态");
        }
        var that = this;
        if (data.id) {
          merMerchantPut(data).then((res) => {
            if (res.code == 200) {
              that.status = false;
              that.$message.success(res.msg);
              that.getList();
            } else {
              that.$message.error(res.msg);
            }
          });
        } else {
          merMerchantPost(data).then((res) => {
            if (res.code == 200) {
              that.status = false;
              that.$message.success(res.msg);
              that.getList();
            } else {
              that.$message.error(res.msg);
            }
          });
        }
      }
      //关闭/启用
      else if (options == 5) {
         let text = value.status === "1" ? "停用" : "启用";
        this.$confirm("确定"+text+"该商户吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          merMerchantPut({
            id: value.id,
            status: value.status == "1" ? "2" : "1",
          }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.getList();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        });
      }
    },
    /**
     * 处理查询请求
     *
     * 将查询参数中的页码重置为1，并调用getList方法获取列表数据
     */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询表单并重新执行查询操作
     */
    resetQuery() {
      this.queryParams = {
        merchantName: null,
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style>
.width_100 {
  width: 100%;
}
</style>
